package com.guldan.jaina.manager;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.citizenbank.CitizenBankDTO;
import com.guldan.jaina.dto.citizenbank.CitizenBankSearchDTO;
import com.guldan.jaina.dto.projecttagrelation.ProjectTagRelationDTO;
import com.guldan.jaina.dto.projecttagrelation.ProjectTagRelationSearchDTO;
import com.guldan.jaina.utils.CitizenInfoDomainConvertUtils;
import com.guldan.jaina.utils.ProjectTagRelationDomainConvertUtils;
import com.guldan.mapper.db.entity.CitizenBank;
import com.guldan.mapper.db.entity.CitizenBankExample;
import com.guldan.mapper.db.entity.ProjectTag;
import com.guldan.mapper.db.entity.ProjectTagRelation;
import com.guldan.mapper.db.entity.ProjectTagRelationExample;
import com.guldan.mapper.db.mapper.ProjectTagMapperExt;
import com.guldan.mapper.db.mapper.ProjectTagRelationMapperExt;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class ProjectTagRelationManager extends BaseManager {

    @Autowired
    private ProjectTagRelationMapperExt projectTagRelationMapperExt;
    
    @Autowired
    private ProjectTagMapperExt projectTagMapperExt;
    
    //获取单个项目标签关联信息
    public ProjectTagRelationDTO get(Integer projectTagRelationId) {
    	ProjectTagRelationDTO dto = ProjectTagRelationDomainConvertUtils.convert(projectTagRelationMapperExt.selectByPrimaryKey(projectTagRelationId));
    	ProjectTag projectTag = projectTagMapperExt.selectByPrimaryKey(dto.getProjectTagId());
    	dto.setProjectTagName(projectTag.getProjectTagName());
    	return dto;
    }

    //新增/更新单个项目标签关联，传入：ProjectTagRelationDTO
    public ProjectTagRelationDTO save(ProjectTagRelationDTO dto) {
    	ProjectTagRelation entity = null;

    	ProjectTagRelationExample example = new ProjectTagRelationExample();
    	ProjectTagRelationExample.Criteria criteria = example.createCriteria();
        if (dto.getProjectId() != null && dto.getProjectId() > 0) {
            criteria.andProjectIdEqualTo(dto.getProjectId());
        }
        if (dto.getProjectTagId() != null && dto.getProjectTagId() > 0) {
            criteria.andProjectTagIdEqualTo(dto.getProjectTagId());
        }
        List<ProjectTagRelation> list = projectTagRelationMapperExt.selectByExample(example);
        boolean needUpdate = false;
        if (list != null && list.size() > 0) {
        	for (ProjectTagRelation relation : list) {
        		if (!relation.getIsDelete()) {
        			entity = relation;
        			needUpdate = true;
        			break;
        		}
        	}
        }
        
        if (needUpdate) {
            entity.setProjectId(dto.getProjectId());
            entity.setProjectTagId(dto.getProjectTagId());
            entity.setIsDelete(false);
            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            projectTagRelationMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new ProjectTagRelation();
            entity.setProjectId(dto.getProjectId());
            entity.setProjectTagId(dto.getProjectTagId());

            entity.setDisabled(dto.getDisabled());
            entity.setCreator(dto.getCreator());
            entity.setCreateTime(DateUtil.getCurrentTime());
            projectTagRelationMapperExt.insertSelective(entity);
        }
        dto = ProjectTagRelationDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除单个项目标签关联信息
    public Boolean delete(ProjectTagRelation entity) {
        entity.setIsDelete(true);
        return projectTagRelationMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }
    
    //分页查询项目标签关联信息列表
    public Pagination<ProjectTagRelationDTO> list(ProjectTagRelationSearchDTO projectTagRelationSearchDTO) {
        Pagination<ProjectTagRelationDTO> result = new Pagination<ProjectTagRelationDTO>(projectTagRelationSearchDTO.getCurrentPage(), projectTagRelationSearchDTO.getPerPage());
        ProjectTagRelationExample example = buildSearchExample(projectTagRelationSearchDTO);
        int total = projectTagRelationMapperExt.countByExample(example);
        result.setTotalCount(total);
        if (total > 0) {
            List<ProjectTagRelationDTO> data = new ArrayList<ProjectTagRelationDTO>();
            List<ProjectTagRelation> list = projectTagRelationMapperExt.selectByExampleWithPaging(example, result.getOffset(), result.getPageSize());
            for (ProjectTagRelation entity : list) {
            	ProjectTagRelationDTO dto = ProjectTagRelationDomainConvertUtils.convert(entity);
                data.add(dto);
            }
            result.setList(data);
        }
        return result;
    }

    /**
     * 根据projectId获取projectTagRelationList
     * @param projectId
     * @return
     */
    public List<ProjectTagRelation> getProjectTagRelationListByProjectId(Integer projectId) {
    	ProjectTagRelationExample example = new ProjectTagRelationExample();
    	ProjectTagRelationExample.Criteria criteria = example.createCriteria();
    	criteria.andProjectIdEqualTo(projectId);
    	criteria.andIsDeleteEqualTo(Boolean.FALSE);
    	List<ProjectTagRelation> projectTagRelationList = projectTagRelationMapperExt.selectByExample(example);
  	
    	return projectTagRelationList;
    }
    
    //build查询Example
    private ProjectTagRelationExample buildSearchExample(ProjectTagRelationSearchDTO search) {
    	ProjectTagRelationExample example = new ProjectTagRelationExample();
    	ProjectTagRelationExample.Criteria criteria = example.createCriteria();
        if (search.getProjectTagRelationId() != null && search.getProjectTagRelationId() > 0) {
            criteria.andProjectTagRelationIdEqualTo(search.getProjectTagRelationId());
        }
        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("project_tag_relation_id asc");
        }
        return example;
    }

    public List<ProjectTagRelationDTO> saveList(Integer projectId, List<ProjectTagRelationDTO> saveDtos) {
        List<ProjectTagRelation> historyList = getProjectTagRelationListByProjectId(projectId);
        if (historyList != null && historyList.size() > 0) {
            for (ProjectTagRelation history : historyList) {
                Boolean needDelete = true;
                for (ProjectTagRelationDTO dto : saveDtos) {
                    if (dto.getProjectTagId() != null && history.getProjectTagId() != null && dto.getProjectTagId().intValue() == history.getProjectTagId().intValue()) {
                        needDelete = false;
                        break;
                    }
                }
                if (needDelete) {
                    delete(history);
                }
            }
        }
        for (ProjectTagRelationDTO dto : saveDtos) {
            dto.setProjectId(projectId);
            save(dto);
        }
        return saveDtos;
    }
    
}
